package xslice

// FindIf returns the index of the first element that satisfies the predicate function.
// If no such element is found, it returns -1.
func FindIf[T any](slice []T, predicate func(T) bool) int {
	for i, elem := range slice {
		if predicate(elem) {
			return i
		}
	}
	return -1
}

// ContainsField checks if any struct within the slice has a field with the specified name and value.
// It uses a predicate to match the desired field and value.
func ContainsField[T any](slice []T, predicate func(T) bool) bool {
	return FindIf(slice, predicate) >= 0
}

// GetFieldSlice returns slice of one field in the struct
func GetFieldSlice[T any, F any](slice []T, getField func(T) F) []F {
	fields := make([]F, len(slice))
	for i, item := range slice {
		fields[i] = getField(item)
	}
	return fields
}
